import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-06-13
 * Time: 9:30
 */
public class commonAncestor {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    public static TreeNode build(String str) {
        if(str == null || str.length() == 0) {
            return null;
        }
        String input = str.replace("[","");
        input = input.replace("]","");
        String[] arr = input.split(",");
        Integer[] num = new Integer[arr.length];
        for(int i = 0; i < arr.length; i++) {
            if(!arr[i].equals("null")) {
                num[i] = Integer.parseInt(arr[i]);
            }else {
                num[i] = null;
            }
        }
        TreeNode root = new TreeNode(num[0]);
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int index = 1;
        while(!q.isEmpty() && index < arr.length) {
            TreeNode node = q.poll();
            if(index < num.length && num[index] != null) {
                node.left = new TreeNode(num[index]);
                q.offer(node.left);
            }
            index++;
            if(index < num.length && num[index] != null) {
                node.right = new TreeNode(num[index]);
                q.offer(node.right);
            }
            index++;
        }
        return root;

    }
    public static TreeNode findNode(TreeNode root,int val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode leftResult = findNode(root.left,val);
        if(leftResult != null) {
            return leftResult;
        }
        return findNode(root.right,val);
    }


    public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        if(p == root || q == root) {
            return root;
        }

        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);

        if(leftTree != null && rightTree != null) {
            return root;
        }

        if(leftTree != null) {
            return leftTree;
        }

        if(rightTree != null) {
            return rightTree;
        }
        return null;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        TreeNode root = build(input);
        // 读取p和q的值
        int pVal = sc.nextInt();
        int qVal = sc.nextInt();
        TreeNode p = findNode(root,pVal);
        TreeNode q = findNode(root,qVal);
        TreeNode res = lowestCommonAncestor(root,p,q);
        System.out.println("结果是: "+res.val);
    }
}

